//
//  HelloWorldLayer.m
//  IAPTest
//
//  Created by mac on 12-4-13.
//  Copyright __MyCompanyName__ 2012年. All rights reserved.
//


// Import the interfaces
#import "HelloWorldLayer.h"

#define kMyFeatureIdentifier @"com.fcplay.gc.buytouch10"

// HelloWorldLayer implementation
@implementation HelloWorldLayer

+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	HelloWorldLayer *layer = [HelloWorldLayer node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init])) {
		
		// create and initialize a Label
		CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];

		// ask director the the window size
		CGSize size = [[CCDirector sharedDirector] winSize];
	
		// position the label on the center of the screen
		label.position =  ccp( size.width /2 , size.height/2 );
		
		// add the label as a child to this Layer
		//[self addChild: label];
        
        CCSprite *sp=[CCSprite spriteWithFile:@"Icon.png"];
        
        sp.position=ccp( size.width /2 , size.height/2 );
        [self addChild:sp];
        
        storesp=sp;
        self.isTouchEnabled=YES;
	}
	return self;
}

-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch* t=[touches anyObject];
    
    CGPoint p=[t locationInView:[t view]];
    if (CGRectContainsPoint(storesp.boundingBoxInPixels, p)) {
        //Purchased;
        
        if ([SKPaymentQueue canMakePayments]) {
            [self requestProductData];
        }
        else{
            
        }

    }
}

- (void) requestProductData
{
    SKProductsRequest *request= [[SKProductsRequest alloc] initWithProductIdentifiers:
                                 [NSSet setWithObject: kMyFeatureIdentifier]];
    request.delegate = self;
    [request start];
}

//- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
//{
//    NSArray *myProducts = response.products;
//    // Populate your UI from the products list.
//    // Save a reference to the products list.
//}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    
    NSArray *arr=[response products];
    
    if (!arr) {
        return;
    }
    NSLog(@"Product Count = %i ",[arr count]);
    if ([arr count]==0) {
        NSLog(@"No Product");
        return;
    }
//    SKProduct *p=[[SKProduct alloc] init];
    
    SKProduct *pro=[arr objectAtIndex:0];
    
    NSLog(@"#######*************###");
    NSLog(@"Price = %f",[[pro price] doubleValue]);
    NSLog(@"productIdentifier = %@",[pro productIdentifier]);
    NSLog(@"localizedDescription= %@",[pro localizedDescription]);
    
    SKPayment *payment=[SKPayment paymentWithProductIdentifier:pro];
    
    [[SKPaymentQueue defaultQueue] addPayment:payment];
    [request autorelease];
}
-(void)request:(SKRequest *)request didFailWithError:(NSError *)error{
    UIAlertView *alerView =  [[UIAlertView alloc] initWithTitle:@"Alert" message:[error localizedDescription]  delegate:nil cancelButtonTitle:NSLocalizedString(@"Close",nil)  otherButtonTitles:nil];
    [alerView show];
    [alerView release]; 
}
-(void)requestDidFinish:(SKRequest *)request{
    NSLog(@"%@",NSStringFromSelector(_cmd));
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
}
@end
